package com.victor.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.victor.common.core.utils.EncryptUtils;
import com.victor.common.dao.system.SysThirdKeyDao;
import com.victor.common.entity.system.SysThirdKey;
import com.victor.system.service.SysThirdKeyService;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.UUID;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author victor
 * @since 2023年06月14日
 */
@Service
public class SysThirdKeyServiceImpl extends ServiceImpl<SysThirdKeyDao, SysThirdKey> implements SysThirdKeyService {

    @Override
    public boolean saveCompany(String company) {
        SysThirdKey body = new SysThirdKey();
        body.setCompany(company);
        Map<String,String > map = EncryptUtils.getSM2Key();
        body.setPublicKey(map.get("publicKey"));
        body.setPrivateKey(map.get("privateKey"));
        body.setHeaderInfo(UUID.randomUUID().toString());
        return save(body);
    }

    @Override
    public boolean updateKeyInfoById(SysThirdKey body) {
        Map<String,String > map = EncryptUtils.getSM2Key();
        body.setPublicKey(map.get("publicKey"));
        body.setPrivateKey(map.get("privateKey"));
        return updateById(body);
    }

    @Override
    public boolean updateThirdToken(SysThirdKey body) {
        LambdaUpdateWrapper<SysThirdKey> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(SysThirdKey::getToken,body.getToken())
                .set(SysThirdKey::getRefreshToken,body.getRefreshToken())
                .eq(SysThirdKey::getCompany,"医丞");
        return update(wrapper);
    }
}
